{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Running PopSyCLE on a Slurm Scheduler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/michael/miniconda3/lib/python3.6/site-packages/pysynphot/locations.py:346: UserWarning: Extinction files not found in /Users/michael/models/cdbs/extinction\n",
      "  warnings.warn('Extinction files not found in %s' % (extdir, ))\n"
     ]
    }
   ],
   "source": [
    "from popsycle import run"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Slurm is an open source, fault-tolerant, and highly scalable cluster management and job scheduling system for large and small Linux clusters (https://slurm.schedmd.com/overview.html). Multiple instances of the PopSyCLE pipeline can be executed in parallel if running on a system that both has PopSyCLE and a slurm scheduler installed. The ``popsycle.run.generate_slurm_scripts`` function will create and submit slurm batch scripts that execute the entire PopSyCLE pipeline.\n",
    "\n",
    "To begin, create a slurm configuration file that contains the linux cluster parameters necessary to submit a batch scripts. These parameters outline features of the slurm scheduler and the config file should only need to be created once. An example can be found in `popsycle/data/slurm_config.yaml` and a custom slurm config file can be generated using ``popsycle.run.generate_slurm_config_file``. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "run.generate_slurm_config_file(config_filename='slurm_config.yaml',\n",
    "                               path_python='python',\n",
    "                               account='uLens',\n",
    "                               queue='regular',\n",
    "                               resource='Haswell',\n",
    "                               n_cores_per_node=32,\n",
    "                               n_nodes_max=2388,\n",
    "                               walltime_max=48,\n",
    "                               additional_lines=['module load cray-hdf5/1.10.5.2', 'export HDF5_USE_FILE_LOCKING=FALSE'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, create a PopSyCLE configuration file that contains the parameters necessary to run the PopSyCLE pipeline. You may want to create a single PopSyCLE configuration file for each project in order to keep the execution of PopSyCLE the same over multiple\n",
    "fields. An example can be found in `popsycle/data/popsycle_config.yaml` and a custom popsycle config file can be generated using ``popsycle.run.generate_popsycle_config_file``. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "run.generate_popsycle_config_file(config_filename='popsycle_config.yaml',\n",
    "                                  radius_cut=2,\n",
    "                                  obs_time=1095,\n",
    "                                  n_obs=10,\n",
    "                                  theta_frac=2,\n",
    "                                  blend_rad=0.75,\n",
    "                                  isochrones_dir='/Users/myself/popsycle_isochrones',\n",
    "                                  bin_edges_number=None,\n",
    "                                  BH_kick_speed_mean=50,\n",
    "                                  NS_kick_speed_mean=400,\n",
    "                                  photometric_system='ubv',\n",
    "                                  filter_name='r',\n",
    "                                  red_law='Schlafly11')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Lastly, run the ``popsycle.run.generate_slurm_scripts`` function along with parameters about a single PopSyCLE field that you would like to run. These parameters could be different for each field and allow for the user to launch multiple instances of PopSyCLE at once by running this function multiple times with\n",
    "different input. A slurm script has been generated in `path_run` that will submit to the compute cluster when `submitFlag=True`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "run.generate_slurm_scripts(slurm_config_filename='slurm_config.yaml',\n",
    "                           popsycle_config_filename='popsycle_config.yaml',\n",
    "                           path_run='/PATH/TO/SLURM_SCRIPT',\n",
    "                           output_root='test',\n",
    "                           longitude = 1.25,\n",
    "                           latitude = -2.65,\n",
    "                           area=0.001,\n",
    "                           n_cores_calc_events=4,\n",
    "                           walltime='01:00:00',\n",
    "                           overwrite=True,\n",
    "                           submitFlag=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The PopSyCLE pipeline can also be run without using slurm scripts using the executable located at `popsycle/run.py`. More details on how to run this executable can be found `in our example notebook ```docs/PopSyCLE_example_run.ipynb``` or by running\n",
    "```python {PATH_TO_POPSYCLE}/popsycle/run.py -h```."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}